package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.dto.loginDTO;
import com.itheima.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/member")
@Api("快速登录")
public class MemberController {
	@Autowired
	private MemberService memberService;
	@Autowired
	private RedisTemplate redisTemplate;

	@PostMapping("/login")
	@ApiOperation("用户快速登录")
	public Result login(@RequestBody loginDTO loginDTO) {
		//验证码是否合法
		Integer validateCode = (Integer) redisTemplate.opsForValue().get(RedisMessageConstant.SENDTYPE_LOGIN + "_" + loginDTO.getTelephone());
		if (validateCode == null ||
				Integer.compare(Integer.valueOf(loginDTO.getValidateCode()), validateCode) != 0) {
			return new Result(false, MessageConstant.VALIDATECODE_ERROR);
		}
		Integer recordCount = memberService.login(loginDTO.getTelephone());
		return new Result(true, MessageConstant.LOGIN_SUCCESS, recordCount);
	}
}

